/*
 * marqdown
 * --------
 * a C# Markdown processor
 * 
 * Copyright (c) 2010 Andrew Wall
 * http://code.google.com/p/marqdown/
 * 
 * 
 */


#region Copyright and license

/*

Copyright (c) 2010 Andrew Wall

 http://www.opensource.org/licenses/mit-license.php
  
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#endregion


using System;
using System.Collections.Generic;

namespace Marqdown
{
    namespace StateMachine
    {
        public class BlockQuote : CompetitiveState
        {
            internal static ConcreteState blockQuoteEOL = new BlockQuoteEOL();
            internal static ConcreteState preBlockQuote = new PreBlockQuote();
            internal static ConcreteState blockQuoteNewLine = new BlockQuoteNewLine();
            internal static ConcreteState postBlockQuote = new PostBlockQuote();
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        // Space is removed
                        NewBlockQuoteTag(info);
                        info.ChangeState(BlockQuote.blockQuoteEOL);
                        break;
                    default:
                        NewBlockQuoteTag(info);
                        info.ChangeState(BlockQuote.blockQuoteEOL);
                        info.process(c);
                        break;
                }
            }
            private void NewBlockQuoteTag(Info info)
            {
                NewTag(info, "blockquote");
                //info.tag.SetClosingPrefix(info.prefix);
            }
            public override void GetAction(ActionSetting a)
            {
                a.leading = ">";
                a.action = Info.nop;
            }
        }
        public class PreBlockQuote : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        // Space is removed
                        info.ChangeState(BlockQuote.blockQuoteEOL);
                        break;
                    default:
                        info.ChangeState(BlockQuote.blockQuoteEOL);
                        info.process(c);
                        break;
                }
            }

        }
        public class BlockQuoteEOL : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        info.tag.NewLine();
                        info.ChangeState(BlockQuote.blockQuoteNewLine);
                        break;
                    default:
                        info.tag.Append(c);
                        break;
                }
            }
        }
        public class BlockQuoteNewLine : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '>':
                        info.ChangeState(BlockQuote.preBlockQuote);
                        break;
                    case '\n':
                        info.ChangeState(BlockQuote.postBlockQuote);
                        break;
                    default:
                        info.tag.Append(c);
                        info.ChangeState(BlockQuote.blockQuoteEOL);
                        break;
                }
            }
        }
        public class PostBlockQuote : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        // Absorb newlines
                        break;
                    case '>':
                        info.ChangeState(BlockQuote.preBlockQuote);
                        break;
                    default:
                        // Terminate <blockquote>
                        info.tag.NewLine();
                        info.tag.SetAppendage("\n\n");
                        // Post process tags list
                        postProcessTag(info);

                        info.NewTag();
                        ///Change to plain, then re-send the char
                        info.ResendAsLeading(c);
                        break;
                }
            }
            private void postProcessTag(Info info)
            {
                // Replay the list of strings into the state machine
                // First remember the blockquote tag
                Tag block = info.tag;
                string prefix = info.prefix;
                info.prefix = prefix + "  ";
                List<string> strings = block.TransferStrings();
                block.Append('\n');
                block.NewLine();
                // 
                info.NewChildTag();
                info.ChangeStateToLeading();
                foreach (string s in strings)
                {
                    foreach (char c in s)
                    {
                        info.process(c);
                    }
                    info.process('\n');
                }

                // 'pop' stuff off the stack
                info.terminate();
                info.prefix = prefix;
                info.ChangeState(BlockQuote.postBlockQuote);
                info.PopChildTag();
            }
            public override void terminate(Info info)
            {
                info.tag.NewLine();
                postProcessTag(info);
            }
        }

    }
}
